package com.sz.util;

import com.sz.pojo.frame.Tiku;
import com.sz.service.frame.TikuService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

public class ToolExcel {

    public static void saveTikulist() {
        //获取根目录
        // E:\项目练习\题库\题库\问答对.xlsx
        ArrayList<Integer> tihaolist = new ArrayList<>();
        ArrayList<String> questionlist = new ArrayList<>();
        ArrayList<String> answerlist = new ArrayList<>();

        TikuService tikuService = new TikuService();
        ArrayList<Tiku> tikulist = new ArrayList<>();

        XSSFWorkbook excel;
        try {
            excel = new XSSFWorkbook("E:\\项目练习\\题库\\题库\\问答对.xlsx");
            XSSFSheet sheet1 = excel.getSheetAt(0);
            int rowNum = sheet1.getLastRowNum();
            for (int i = 1; i <= rowNum; i++) {
                Tiku t = new Tiku();
                XSSFRow row = sheet1.getRow(i);
                if (row != null) {
                    // 获取到Excel文件中的所有的列
                    int cells = row.getPhysicalNumberOfCells();
                    for (int j = 1; j < cells; j++) {
                        // 获取到列的值
                        Cell cell = row.getCell(j);
                        if (cell != null) {
                            switch (cell.getCellTypeEnum()) {
                                case STRING:
                                    if (j == 1) {
                                        t.setQuesetion(cell.getStringCellValue());
                                    }
                                    if (j == 2) {
                                        t.setAnswer(cell.getStringCellValue());
                                    }
                                    if (j > 2) {
                                        System.out.println("j大于2");
                                    }
                                    break;
                                default:
                                    if (j == 0) {
                                        tihaolist.add(Double.valueOf(cell.getNumericCellValue()).intValue());
                                    } else {
                                        System.out.println("值为：" + cell.toString());
                                    }
                                    break;
                            }
                        }

                    }
                }
                tikulist.add(t);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        tikulist.forEach((tiku) -> {
            if (ToolStr.isEmpty(tiku.getQuesetion()) && ToolStr.isEmpty(tiku.getAnswer())) {
                tiku.setTime(new Date());
                System.out.print(tiku.getQuesetion() + " ");
                System.out.print(tiku.getAnswer() + " ");
                
                tikuService.save(tiku);
            }
        });
    }

    public static void main(String arg[]) {

    }

}
